<template>
 <div class="login-wrap">
  <el-form  status-icon :model="form" :rules="rules"  ref="ruleForm" label-width="100px" class="login-from">
    <h2>用户注册</h2>
    <el-form-item label="用户名" prop="username">
      <el-input type="text" v-model="form.username"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="password">
      <el-input type="password" v-model="form.password" ></el-input>
    </el-form-item>
    <el-form-item label="确认密码" prop="password2">
      <el-input type="password" v-model="form.password2" show-password ></el-input>
    </el-form-item>
    <el-form-item label="验证邮箱" prop="email">
      <el-input type="email"  v-model="form.email" ></el-input>
    </el-form-item>
    <el-form-item label="邮箱验证码" prop="code">
      <el-input style="width: 180px;" v-model="form.code" ></el-input>
            <el-button id="send" :disabled="flag" @click="sendemail()" > 获取验证码</el-button>
    </el-form-item>
    <el-form-item>
      <router-link to="login"><el-button type="primary">去登录</el-button></router-link>
      <el-button type="primary" @click="submit('ruleForm')">注册</el-button>
    </el-form-item>
  </el-form>
 </div>
</template>

<script>
// import Axios from 'axios'

export default {
  data () {
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.form.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    var checkEmail = (rule, value, callback) => {
      const mailReg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/
      if (!value) {
        return callback(new Error('邮箱不能为空'))
      }
      setTimeout(() => {
        if (mailReg.test(value)) {
          callback()
        } else {
          callback(new Error('请输入正确的邮箱格式'))
        }
      }, 100)
    }
    return {
      flag: false,
      form: {
        username: '',
        password: '',
        password2: '',
        email: '',
        code: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
        ],
        password2: [
          {validator: validatePass2, tagger: 'blur'}
        ],
        email: [
          { validator: checkEmail, tagger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入验证码', trigger: 'blur' },
          { min: 6, max: 6, message: '长度为6位', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    submit (ruleForm) {
      this.$refs[ruleForm].validate((valid) => {
        var data = this.$qs.stringify(this.form)
        if (valid) {
          this.$http.post('http://120.27.129.163/iblog/index.php/register', data)
            .then(backdata => {
              var data = backdata.data
              if (data.code === 200) {
                this.$message({
                  showClose: true,
                  message: data.msg,
                  type: 'success'
                })
                this.$router.push({name: 'Login'})
              } if (data.code === 400) {
                this.$message({
                  showClose: true,
                  message: data.msg,
                  type: 'error'
                })
              } if (data.code === 501) {
                this.$message.error(data.msg)
              }
            })
        } else {
          return false
        }
      })
    },
    sendemail (ruleForm) {
      var data = this.$qs.stringify(this.form)
      this.$http.post('http://Ci.com/sendmail', data).then(backdata => {
        var data = backdata.data
        if (data.code === 200) {
          this.flag = true
          var t = parseInt(59)
          var time = setInterval(function () {
            document.getElementById('send').innerHTML = t + ' s'
            t--
            if (t === 0) {
              this.flag = false
              document.getElementById('send').innerHTML = '获取验证码'
              clearInterval(time)
            }
          }, 1000)
        } else {
          this.$message.error(data.msg)
        }
      })
    }
  }
}
</script>

<style>
  .login-wrap {
  background-color: #324152;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  }
  .login-wrap .login-from {
  background-color: #fff;
  width: 400px;
  padding: 30px;
  border-radius: 5px;
  }
  .login-wrap .login-from .login-btn {
  width: 100%;
  }
</style>
